package com.example.administrator.recyclerview_114dxl.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.example.administrator.recyclerview_114dxl.adapter.MyAdapter;
import com.example.administrator.recyclerview_114dxl.bean.ArticleBean;
import com.example.administrator.recyclerview_114dxl.iface.ArticleListener;
import com.example.administrator.recyclerview_114dxl.model.ArticleModel;
import com.example.administrator.recyclerview_114dxl.R;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;
    private List<ArticleBean> list = null;


    ArticleListener articleListener = new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> beanList) {
            list = beanList;//拿到文章数据
            myAdapter.setList(list);
            myAdapter.notifyDataSetChanged();//通知更新

        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(MainActivity.this, "失败" +msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initRecyclerView();

        ArticleModel model = new ArticleModel();
        model.getResultList("article",1,"rv1l2tidbomann2qn86nln6ur2",articleListener);
    }

    private void initRecyclerView() {
        recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
        layoutManager= new LinearLayoutManager(this);//创建布局
        recyclerView.setLayoutManager(layoutManager);//设置布局
        recyclerView.setHasFixedSize(true);
        //list=getData();
        myAdapter= new MyAdapter(this);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);

    }
    /*
    private List<ArticleBean> getData()
    {
        List<ArticleBean>list = new ArrayList<ArticleBean>();
        ArticleBean articleBean;
        articleBean = new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题1");
        articleBean.setDescription("描述1");
        articleBean.setTime("2017-4-1");
        articleBean.setImgid(R.drawable.f1);
        list.add(articleBean);

        articleBean = new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题2");
        articleBean.setDescription("描述2");
        articleBean.setTime("2017-4-2");
        articleBean.setImgid(R.drawable.f2);
        list.add(articleBean);

        articleBean = new ArticleBean();
        articleBean.setArticleId(1);
        articleBean.setTitle("标题3");
        articleBean.setDescription("描述3");
        articleBean.setTime("2017-4-3");
        articleBean.setImgid(R.drawable.f3);
        list.add(articleBean);


        return list;
    }
    */

}
